package com.fenghuajueli.module_user.ui.widget

import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeightIn
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.fenghuajueli.libbasecoreui.BaseApplication
import com.fenghuajueli.libbasecoreui.feedback.FeedbackPrevActivity
import com.fenghuajueli.libbasecoreui.ui.CustomerServiceActivity
import com.fenghuajueli.libbasecoreui.utils.JumpActivityUtils
import com.fenghuajueli.module_user.R

/**
 * @Author: sz_ijj
 * @Date: 2024-03-30 10:41
 **/
fun Modifier.noRippleClickable(enabled: Boolean = true, onClick: () -> Unit): Modifier =
    composed {
        this.clickable(
            enabled = enabled,
            interactionSource = remember {
                MutableInteractionSource()
            },
            indication = null
        ) { onClick() }
    }

@Composable
internal fun UserTopBarWidget(
    primary: Color = Color.Transparent,
    onPrimary: Color = Color.Black,
    title: String = "",
    topPadding: Dp = 50.dp,
    onBackClick: () -> Unit
) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(primary)
            .padding(top = topPadding, bottom = 11.dp)
    ) {
        AsyncImage(
            modifier = Modifier
                .align(Alignment.CenterStart)
                .padding(start = 23.dp)
                .noRippleClickable {
                    onBackClick()
                },
            model = R.mipmap.user_icon_shapewhite_01,
            colorFilter = ColorFilter.tint(onPrimary),
            contentDescription = null
        )

        Text(
            modifier = Modifier
                .align(Alignment.Center),
            text = title,
            color = onPrimary,
            fontSize = 18.sp,
            fontWeight = FontWeight.Bold
        )
    }
}

@Composable
fun HelpCenterBottomWidget(
    modifier: Modifier
) {
    val context = LocalContext.current

    val darkTheme by rememberUpdatedState(
        newValue = BaseApplication.getApplication()
            .getDayNightMode() == AppCompatDelegate.MODE_NIGHT_YES
    )

    Box(
        modifier = modifier
            .fillMaxWidth()
    ) {
        AsyncImage(
            modifier = Modifier
                .fillMaxWidth()
                .requiredHeightIn(max = 130.dp),
            model = if (darkTheme) R.mipmap.module_user_bg_price2 else R.mipmap.module_user_bg_price,
            contentDescription = null,
            contentScale = ContentScale.FillWidth
        )

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .align(Alignment.BottomCenter)
                .padding(bottom = 30.dp)
        ) {
            HelpCenterBottomItem(
                modifier = Modifier
                    .weight(1f)
                    .noRippleClickable {
                        JumpActivityUtils.goNormalActivity(context, FeedbackPrevActivity::class.java)
                    },
                icon = R.mipmap.module_user_icon_yjfk,
                title = "意见反馈",
                desc = "提供您宝贵的建议"
            )

            HelpCenterBottomItem(
                modifier = Modifier
                    .weight(1f)
                    .noRippleClickable {
                        CustomerServiceActivity.start(context)
                    },
                icon = R.mipmap.module_user_icon_lxkf,
                title = "联系客服",
                desc = "工作日 9:00-18:00"
            )
        }
    }
}

@Composable
private fun HelpCenterBottomItem(
    modifier: Modifier = Modifier,
    icon: Int,
    title: String,
    desc: String
) {
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            AsyncImage(
                modifier = Modifier
                    .padding(start = 7.dp, end = 8.dp)
                    .size(16.dp),
                model = icon,
                contentDescription = null,
                contentScale = ContentScale.FillBounds,
                colorFilter = ColorFilter.tint(color = MaterialTheme.colorScheme.onPrimary)
            )

            Text(
                text = title,
                color = MaterialTheme.colorScheme.onSurface,
                fontSize = 14.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier
            )
        }

        Spacer(modifier = Modifier.height(7.dp))

        Text(
            text = desc,
            color = MaterialTheme.colorScheme.onTertiary,
            fontSize = 12.sp,
            modifier = Modifier
        )
    }
}